{extend name="../../base/view/common/base" /}
<!-- 主体 -->
{block name="body"}
<div class="body-table" style="margin-bottom:0;">
	<div class="layui-form-bar">
		<button class="layui-btn layui-btn-normal layui-btn-sm add-menu">+ 添加部门</button>
	</div>
	<div>
		<table class="layui-hide" id="treeTable" lay-filter="treeTable"></table>
	</div>
</div>
{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
<script>
	function init(layui) {
	var treeGrid = layui.treeGrid,
		layer = layui.layer,
		rightpage = layui.rightpage,
		ptable = treeGrid.render({
			id: 'treeTable'
			, elem: '#treeTable'
			, idField: 'id'
			, url: "/user/department/index"
			, cellMinWidth: 80
			, treeId: 'id'//树形id字段名称
			, treeUpId: 'pid'//树形父id字段名称
			, treeShowName: 'title'//以树形式显示的字段
			, height: 'full-0'
			,isOpenDefault:true
			, cols: [[
				 { field: 'id', width: 100, title: 'ID号', align: 'center' }
				, { field: 'pid', title: '上级部门ID',width: 120, align: 'center'}
				, { field: 'title', title: '部门名称'}
				, { field: 'leader', title: '部门负责人',width: 120, align: 'center'}
				, { field: 'phone', title: '部门电话',width: 160,}
				, { width:180, title: '操作', align: 'center', templet: function (d) {
						var html = '<span class="layui-btn-group"><button class="layui-btn layui-btn-normal layui-btn-xs" lay-event="add">添加下级部门</button><button class="layui-btn layui-btn-xs" lay-event="edit">编辑</button><button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</button></span>';
						return html;
					}
				}
			]]
			, page: false
		});
		
		//表头工具栏事件
		$('.body-table').on('click','.add-menu', function(){
			rightpage.open("/user/department/add",'900px');
			return;
		});

		//操作按钮
		treeGrid.on('tool(treeTable)', function (obj) {
			if (obj.event === 'add') {
				rightpage.open("/user/department/add?pid="+obj.data.id,'900px');
				return;
			}
			if (obj.event === 'edit') {
				rightpage.open("/user/department/add?id="+obj.data.id,'900px');
				return;
			}
			if (obj.event === 'del') {
				layer.confirm('确定要删除吗?', { icon: 3, title: '提示' }, function (index) {
					$.ajax({
						url: "/user/department/delete",
						type: 'post',
						data: { id: obj.data.id },
						success: function (e) {
							layer.msg(e.msg);
							if (e.code == 0) {
								obj.del();
							}
						}
					})
					layer.close(index);
				});
			}
		});
	}
</script>
{include file="../../base/view/common/layui" base="base" extend="['treeGrid','rightpage']" callback="init" /}
{/block}
<!-- /脚本 -->